package cn.java86.myschool.web;

import cn.java86.myschool.entity.AjaxResult;
import cn.java86.myschool.entity.Page;
import cn.java86.myschool.entity.Student;
import cn.java86.myschool.service.GradeServiceI;
import cn.java86.myschool.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private GradeServiceI gradeService;

    @GetMapping("list")
    public ModelAndView paging(
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "sname", required = false) String sname,
            @RequestParam(name = "qq", required = false) String qq,
            @RequestParam(name = "phone", required = false) String phone,
            @RequestParam(name = "enterStart", required = false) Date enterStart,
            @RequestParam(name = "enterEnd", required = false) Date enterEnd) {

        ModelAndView mv = new ModelAndView();
        Page<Student> p = studentService.paging(pageNo, pageSize, sname, qq, phone, enterStart, enterEnd);
        mv.addObject("p", p);
        mv.addObject("sname", sname);
        mv.addObject("qq", qq);
        mv.addObject("phone", phone);
        mv.addObject("enterStart", enterStart);
        mv.addObject("enterEnd", enterEnd);
        mv.setViewName("student/list");
        return mv;
    }

    //
    @Value("${upload-path}")
    private String FOLDER;// = "D:/tmp";

    @PostMapping("add")
    public String add(Student stu, @RequestParam("idcard") MultipartFile m, RedirectAttributes attributes) {
        if (!(m == null || m.isEmpty())) {
            //原始的文件名
            String fileName = m.getOriginalFilename();
            //获得后缀
            String suffix = fileName.substring(fileName.lastIndexOf('.'));
            //新的文件名
            String newName = UUID.randomUUID() + suffix;
            try {
                m.transferTo(new File(FOLDER, newName));
                stu.setIdcardpic(newName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.studentService.add(stu);
        attributes.addFlashAttribute("msg", "添加成功");
        return "redirect:/student/list";//重定向
        // return "fail";
    }

    @GetMapping("del/{stuno}")
    public String del(@PathVariable("stuno") long stuno, RedirectAttributes attr) {
        int r = this.studentService.del(stuno);
        attr.addFlashAttribute("msg", r > 0 ? "删除成功" : "删除失败");
        return "redirect:/student/list";
    }

    @GetMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("grades", gradeService.findAll());

        return "student/add";
    }
    @RequestMapping("checkPhone")
    @ResponseBody//返回json格式数据
    public AjaxResult checkPhone(String phone){
        boolean b= this.studentService.checkPhone(phone);
        return b?AjaxResult.error(501,"手机号已经存在"):AjaxResult.success();
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        //所有的Date类型用一种格式
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));

        //binder.registerCustomEditor(Date.class,"birthday", new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd
        // hh:mm"), true));

    }
}
